package com.yunquan.ohana.db

import android.annotation.SuppressLint
import android.content.ContentValues
import android.database.Cursor
import com.yunquan.ohana.entity.LocalPhotoFrames

class DBPhotoFrames {
    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { DBPhotoFrames() }
    }

    private val readDB = DBManager.instance.readableDatabase
    private val writeDB = DBManager.instance.writableDatabase

    fun saveOrUpdate(data: LocalPhotoFrames): Boolean {
        return if (data.id != null) {
            update(data)
        } else {
            insert(data)
        }
    }

    fun insert(data: LocalPhotoFrames): Boolean {
        val result = writeDB.insert(DBManager.TABLE_PHOTO_FRAMES, null, toContentValues(data))
        return result != -1L
    }

    fun update(data: LocalPhotoFrames): Boolean {
        if (data.id == null) return false
        val result = writeDB.update(DBManager.TABLE_PHOTO_FRAMES, toContentValues(data), "id = ?", arrayOf(data.id.toString()))
        return result != 0
    }

    fun updateStatus(groupId: String, status: Int): Boolean {
        val values = ContentValues()
        values.put("status", status)
        val result = writeDB.update(DBManager.TABLE_PHOTO_FRAMES, values, "group_id = ?", arrayOf(groupId))
        return result != 0
    }

    fun delete(id: Int): Boolean {
        val result = writeDB.delete(DBManager.TABLE_PHOTO_FRAMES, "id = ?", arrayOf(id.toString()))
        return result != 0
    }

    fun delete(groupId: String):Boolean{
        val result = writeDB.delete(DBManager.TABLE_PHOTO_FRAMES, "group_id = ?", arrayOf(groupId))
        return result != 0
    }

    fun selectOne(id: Int): LocalPhotoFrames? {
        val cursor = readDB.query(DBManager.TABLE_PHOTO_FRAMES, null, "id = ?", arrayOf(id.toString()), null, null, null)
        var data: LocalPhotoFrames? = null
        if (cursor.moveToFirst()) {
            data = toEntity(cursor)
        }
        cursor.close()
        return data
    }

    fun selectByGroupId(groupId: String): List<LocalPhotoFrames> {
        val cursor = readDB.query(DBManager.TABLE_PHOTO_FRAMES, null, "group_id = ?", arrayOf(groupId), null, null, null)
        val data = mutableListOf<LocalPhotoFrames>()
        while (cursor.moveToNext()) {
            data.add(toEntity(cursor))
        }
        cursor.close()
        return data
    }

    fun selectAll(selection: String?, args: Array<String>?): List<LocalPhotoFrames> {
        val cursor = readDB.query(DBManager.TABLE_PHOTO_FRAMES, null, selection, args, null, null, null)
        val data = mutableListOf<LocalPhotoFrames>()
        while (cursor.moveToNext()) {
            data.add(toEntity(cursor))
        }
        cursor.close()
        return data
    }

    @SuppressLint("Range")
    fun toEntity(cursor: Cursor): LocalPhotoFrames {
        val id = cursor.getInt(cursor.getColumnIndex("id"))
        val groupId = cursor.getString(cursor.getColumnIndex("group_id"))
        val framesId = cursor.getString(cursor.getColumnIndex("frames_id"))
        val status = cursor.getInt(cursor.getColumnIndex("status"))
        return LocalPhotoFrames(
            id = id,
            groupId = groupId,
            framesId = framesId,
            status = status,
        )
    }

    private fun toContentValues(data: LocalPhotoFrames): ContentValues {
        val values = ContentValues()
        if (data.id != null) {
            values.put("id", data.id)
        }
        if (data.groupId != null) {
            values.put("group_id", data.groupId)
        }
        if (data.framesId != null) {
            values.put("frames_id", data.framesId)
        }
        if (data.status != null) {
            values.put("status", data.status)
        }
        return values
    }
}